function GetQueryString(key) {
	// key为url变量名称
	var urlGets = location.search.substr(1);
	if(urlGets.length > 0) {
		start = urlGets.indexOf(key + '=');
		if(start != -1) {
			start = start + key.length + 1;
			end = urlGets.indexOf('&', start)
			if(end == -1) {
				end = urlGets.length
			}
			return decodeURIComponent(urlGets.substring(start, end))
		}
	}
	return '';
}

var isios = 0;
//判断终端开始
var browser = {
	versions: function() {
		var u = navigator.userAgent,
			app = navigator.appVersion;
		return {
			trident: u.indexOf('Trident') > -1, //IE内核
			presto: u.indexOf('Presto') > -1, //opera内核
			webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
			gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
			mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
			ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
			android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
			iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
			iPad: u.indexOf('iPad') > -1, //是否iPad
			webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
			weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
			qq: u.match(/\sQQ/i) == " qq" //是否QQ
		};
	}(),
	language: (navigator.browserLanguage || navigator.language).toLowerCase()
}
if(browser.versions.ios) {
	isios = 1;
}
//判断终端开始
function mtime(na) {
	var num = 60;
	var ff = setInterval(function() {
		num--;
		na.disabled = true;
		na.innerHTML = num + 's后尝试';
		na.style.background = 'gray';
		if(num == 0) {
			clearInterval(ff);
			na.disabled = false;
			na.innerHTML = '获取验证码';
			na.style.background = '#F26100';
		}
	}, 1000)
}

var httpAdress ="http://dkx.yytaomeng.com";
//var httpAdress ="http://192.168.0.123:8080";
var regButton = document.getElementById('reg'); //注册按钮
var accountBox = document.getElementById('phone'); //手机号
var reg_password = document.getElementById('password'); //密码
var checknum = document.getElementById('num'); //验证码
//  var mhide = document.getElementById('hide');
//	var forgetButton = document.getElementById('forgetPassword');
var change = true;
var getChecknum = document.getElementById('get-checknum');
//获取验证码按钮事件按钮效果
getChecknum.addEventListener('touchstart', function(event) {
	getChecknum.style.background = '#F0AD4E'
});
getChecknum.addEventListener('touchend', function(event) {
	getChecknum.style.background = '#F26100'
});
//  获取验证码按钮事件
getChecknum.addEventListener('tap', function(event) {
	console.log(1)
	var network = true;
	if(network) {
		if(accountBox == "") {
			mui.toast("手机号不能为空");
			return;
		}
		var check_phone_number = /^1([38]\d|4[57]|5[0-35-9]|7[06-8]|8[89])\d{8}$/;
		if(accountBox.value.length == 0) {
			mui.toast("手机号不能为空");
			return;
		}
		if(accountBox.value.length != 11) {
			mui.toast("请输入有效的手机号！");
			return;
		}
		if(!accountBox.value.match(check_phone_number)) {
			mui.toast("请输入有效的手机号！");
			return;
		}
	}
	//密码校验
	var reg_password = document.getElementById("password").value;
	if(reg_password.length == 0) {
		mui.toast("密码不能为空");
		return;
	}

	if(reg_password.length < 6) {
		mui.toast("密码长度不能少于6位！");
		return;
	}
	if(reg_password.length > 16) {
		mui.toast("密码长度不能超过16位！");
		return;
	}
	console.log(accountBox.value);
	//倒计时函数
	//		mtime(getChecknum);	
	$.ajax({
		type: 'get',
		url: httpAdress + '/api/user/getSMSCode',
		data: {
			'phone': accountBox.value,
			'type': 1
		},
		contentType: "application/json; charset=utf-8",
		dataType: 'jsonp',
		jsonp: "callback",
		jsonpCallback: "successCallback",
		success: function(data) {
			if(data.status == 1) {
				mtime(getChecknum);
			}
			mui.toast(data.msg);
		},
		error: function(error) {
			mui.toast("获取失败，请重试！");
			getChecknum.style.background = '#F26100';
		}
	})
})

regButton.addEventListener('tap', function(event) {
	var network = true;
	if(network) {
		if(accountBox == "") {
			mui.toast("手机号不能为空");
			return;
		}
		var check_phone_number = /^1([38]\d|4[57]|5[0-35-9]|7[06-8]|8[89])\d{8}$/;
		if(accountBox.value.length == 0) {
			mui.toast("手机号不能为空");
			return;
		}
		if(accountBox.value.length != 11) {
			mui.toast("请输入有效的手机号！");
			return;
		}
		if(!accountBox.value.match(check_phone_number)) {
			mui.toast("请输入有效的手机号！");
			return;
		}
	}
	//密码校验
	var reg_password = document.getElementById("password").value;
	if(reg_password.length == 0) {
		mui.toast("密码不能为空");
		return;
	}
	if(checknum.value.length == 0) {
		mui.toast("请输入验证码");
		return;
	}
	if(reg_password.length < 6) {
		mui.toast("密码长度不能少于6位！");
		return;
	}
	if(reg_password.length > 16) {
		mui.toast("密码长度不能超过16位！");
		return;
	} else {
		var abc = GetQueryString("abc");

//			console.log(ss);
			
			var data = {};
			data["phone"] = accountBox.value;
			data["passwd"] = reg_password;
			data["channel"] = 3;
			data["checknum"] = checknum.value;
			data["type"] =1;
			data["abc"] = abc;
			data["validcode"] = 0;
			console.log(data);
//			alert(ss);
			$.ajax({
			type: 'get',
			url: httpAdress + '/api/user/register',
			data: data,
			contentType: "application/json; charset=utf-8",
			dataType: 'jsonp',
			jsonp: "callback",
			timeout: 5000,
			jsonpCallback: "successCallback",
			success: function(data) {

				console.log(data.status);
				if(data.status == 1) {
					mui.toast("注册成功！");
//					if(isios == 1) {
//						//				  	是ios
//						window.location.href = 'https://itunes.apple.com/cn/app/jie-kuan-wang-you-xin-yong/id1136185453?mt=8';
//					} else {
						window.location.href = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.zqxq.daikuanxia';
//					}
				} else {
					mui.toast("注册失败，请重试!");
				}
			},
			error: function(error) {
				mui.toast("注册失败，请重试！");
			}
		});	

	//
	//		window.addEventListener('resize', function() {
	//			oauthArea.style.display = document.body.clientHeight > 400 ? 'block' : 'none';
	//		}, false);

	}
});